package com.cow.controller;

import com.cow.entity.Discussions;
import com.cow.service.DiscussionService;
import com.cow.util.general.CommonResult;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@CrossOrigin
@RestController
@Slf4j
public class DiscussionController {
    @Autowired
    private DiscussionService discussionService;

    // 获取全部讨论（联表查询用户信息）
    @GetMapping("/discussion/list")
    public CommonResult findAll() {
        try {
            return CommonResult.success("查询成功", discussionService.selectAll());
        } catch (Exception e) {
            log.error("查询失败: ", e);
            return CommonResult.error("服务器错误");
        }
    }


    @PostMapping("/discussion/create")
    public CommonResult create(@RequestBody Discussions discussions) {
        if (discussionService.insertData(discussions)){
            return CommonResult.success("发表成功", discussions);
        }
        return CommonResult.error("发表失败");
    }

    @DeleteMapping("/discussion/delete/{id}")
    public CommonResult delete(@PathVariable Integer id) {

        if (discussionService.deleteById(id)) {
            return CommonResult.success("删除成功",id);
        }
        return CommonResult.error("删除失败");
    }
}